package com.app.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {

    private static final String ALGORITHM_MD5 = "MD5";
    public static final String MD5_32bit(String readyEncryptStr) throws NoSuchAlgorithmException {
        if(readyEncryptStr != null){
            //Get MD5 digest algorithm's MessageDigest's instance.
            MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
            //Use specified byte update digest.
            md.update(readyEncryptStr.getBytes());
            //Get cipher text
            byte [] b = md.digest();
            //The cipher text converted to hexadecimal string
            StringBuilder su = new StringBuilder();
            //byte array switch hexadecimal number.
            for(int offset = 0,bLen = b.length; offset < bLen; offset++){
                String haxHex = Integer.toHexString(b[offset] & 0xFF);
                if(haxHex.length() < 2){
                    su.append("0");
                }
                su.append(haxHex);
            }
            return su.toString();
        }else{
            return null;
        }
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        String s="sjk_prd_123456789"+String.valueOf(System.currentTimeMillis())+"a!s1d@f2g#h3j$k4l%;5'^6&7*8*";
        System.out.println(s);
        String asfasfas = MD5_32bit("sjk_prd_123456789"+String.valueOf(System.currentTimeMillis())+"a!s1d@f2g#h3j$k4l%;5'^6&7*8*");
        System.out.println(asfasfas);

    }

}
